{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "002a8881",
   "metadata": {},
   "source": [
    "---\n",
    "* 时间：2022-10-25\n",
    "* 李锦鹏\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "900e6cb4",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "> 1. while循环：while Ture-->无限循环\n",
    ">>. 1.指定次数，while Ture可以实现for循环的指定次数，增加一个count计数器即可。  \n",
    ">>. 2.while Ture 一般与 用户输入 一起使用，让用户可以重复输入，知道某些条件成立结束输入（购物车）  \n",
    "\n",
    "> 2.购物车实践（平时作业）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8acd4ce3",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "> 1.元组  \n",
    "> 2.集合  \n",
    "> 3.字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "866709ff",
   "metadata": {},
   "source": [
    "# 1.元组（tuple）\n",
    "> 1.元组的基本内容（形态，定义）  \n",
    "> 2.元组的性质\n",
    ">> 1.有序性（indexing,sliceing）  \n",
    ">> 2.<font color=\"red\">**不可变性**</font>,元组没有增、删、改  \n",
    "\n",
    "> 3.元组的一般方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5052744a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(200000, 200001, 200002) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "网新_学号 = (200000,200001,200002)\n",
    "print(网新_学号,type(网新_学号))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "08a2725f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(200000, 200001, 200002) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "网新_学号 = 200000,200001,200002\n",
    "print(网新_学号,type(网新_学号))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5c88609a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200000"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新_学号[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a15b2b59",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(200001, 200002)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新_学号[1:]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1d40b06",
   "metadata": {},
   "source": [
    "## 元组的实际案例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "24133d42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=10, tm_mday=25, tm_hour=15, tm_min=6, tm_sec=36, tm_wday=1, tm_yday=298, tm_isdst=0)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "eca4f837",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on class tuple in module builtins:\n",
      "\n",
      "class tuple(object)\n",
      " |  tuple(iterable=(), /)\n",
      " |  \n",
      " |  Built-in immutable sequence.\n",
      " |  \n",
      " |  If no argument is given, the constructor returns an empty tuple.\n",
      " |  If iterable is specified the tuple is initialized from iterable's items.\n",
      " |  \n",
      " |  If the argument is a tuple, the return value is the same object.\n",
      " |  \n",
      " |  Built-in subclasses:\n",
      " |      asyncgen_hooks\n",
      " |      UnraisableHookArgs\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __add__(self, value, /)\n",
      " |      Return self+value.\n",
      " |  \n",
      " |  __contains__(self, key, /)\n",
      " |      Return key in self.\n",
      " |  \n",
      " |  __eq__(self, value, /)\n",
      " |      Return self==value.\n",
      " |  \n",
      " |  __ge__(self, value, /)\n",
      " |      Return self>=value.\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __getitem__(self, key, /)\n",
      " |      Return self[key].\n",
      " |  \n",
      " |  __getnewargs__(self, /)\n",
      " |  \n",
      " |  __gt__(self, value, /)\n",
      " |      Return self>value.\n",
      " |  \n",
      " |  __hash__(self, /)\n",
      " |      Return hash(self).\n",
      " |  \n",
      " |  __iter__(self, /)\n",
      " |      Implement iter(self).\n",
      " |  \n",
      " |  __le__(self, value, /)\n",
      " |      Return self<=value.\n",
      " |  \n",
      " |  __len__(self, /)\n",
      " |      Return len(self).\n",
      " |  \n",
      " |  __lt__(self, value, /)\n",
      " |      Return self<value.\n",
      " |  \n",
      " |  __mul__(self, value, /)\n",
      " |      Return self*value.\n",
      " |  \n",
      " |  __ne__(self, value, /)\n",
      " |      Return self!=value.\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  __rmul__(self, value, /)\n",
      " |      Return value*self.\n",
      " |  \n",
      " |  count(self, value, /)\n",
      " |      Return number of occurrences of value.\n",
      " |  \n",
      " |  index(self, value, start=0, stop=9223372036854775807, /)\n",
      " |      Return first index of value.\n",
      " |      \n",
      " |      Raises ValueError if the value is not present.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Class methods defined here:\n",
      " |  \n",
      " |  __class_getitem__(...) from builtins.type\n",
      " |      See PEP 585\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(tuple)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "22b9b93e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([1, 2, 3, 4], [3, 4, 5, 6]) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "tuple_test = ([1,2,3,4],[3,4,5,6])\n",
    "print(tuple_test,type(tuple_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "270dc220",
   "metadata": {},
   "outputs": [],
   "source": [
    "tuple_test[0].append(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f3194f51",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([1, 2, 3, 4, 100], [3, 4, 5, 6])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tuple_test"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3dc07a89",
   "metadata": {},
   "source": [
    "# 2.集合\n",
    "> 1.集合的基本概念  \n",
    ">> 1.没有重复项  \n",
    ">> 2.无序性\n",
    "\n",
    "> 2.集合的一般方法\n",
    ">> 1.集合的运算：& ，  \n",
    ">> 2.集合的一般方法：intersection，"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a149cf5a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'orange', 'apple', 'pear', 'orange', 'banana']\n",
      "{'pear', 'apple', 'banana', 'orange'}\n"
     ]
    }
   ],
   "source": [
    "basket_lists = ['apple','orange','apple','pear','orange','banana']\n",
    "basket_sets = {'apple','orange','apple','pear','orange','banana'}\n",
    "print(basket_lists)\n",
    "print(basket_sets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "6dfdce7d",
   "metadata": {},
   "outputs": [],
   "source": [
    "电竞社 = {1,2,3,4,5,6,7,8,9}\n",
    "武术队 = {7,8,9,10,11,12,13}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37a18cfd",
   "metadata": {},
   "source": [
    "* 交集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "94e6432a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{7, 8, 9}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "电竞社 & 武术队"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "60ef81d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{7, 8, 9}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "电竞社.intersection(武术队)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7412f42",
   "metadata": {},
   "source": [
    "* 并集\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "7c486e48",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "电竞社 | 武术队"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "af3b9a67",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(电竞社 | 武术队)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "de08e1f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "电竞社.union(武术队)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a480b1a1",
   "metadata": {},
   "source": [
    "# 3.字典（dictionary）\n",
    "> 1.字典的基本概念\n",
    ">> 1.字典性质  \n",
    ">>> 1.无序性  \n",
    ">>> 2.keys不可变  \n",
    ">>> 3.keys是唯一的  \n",
    "\n",
    ">> 2.字典的形态  \n",
    "\n",
    "> 2.字典的一般方法  \n",
    ">> 1.字典的创建  \n",
    ">> 2.字典的函数和方法  \n",
    ">>> 1.增加  \n",
    ">>> 2.删除  \n",
    ">>> 3.修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "1f94f5d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "奥特曼成员 = {\n",
    "    10001:\"迪迦\",\n",
    "    10002:\"泰罗\",\n",
    "    10003:\"塞罗\",\n",
    "    10004:\"梦比优斯\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "4908e496",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{10001: '迪迦', 10002: '泰罗', 10003: '塞罗', 10004: '梦比优斯'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(奥特曼成员,type(奥特曼成员))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "f3121448",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 因为序号是唯一的，可以作为字典的key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "6ce195c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{10001: '迪迦', 10002: '泰罗', 10003: '塞罗', 10004: '梦比优斯', 10005: '迪迦'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "奥特曼成员 = {\n",
    "    10001:\"迪迦\",\n",
    "    10002:\"泰罗\",\n",
    "    10003:\"塞罗\",\n",
    "    10004:\"梦比优斯\",\n",
    "    10005:\"迪迦\"\n",
    "}\n",
    "print(奥特曼成员,type(奥特曼成员))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "195c30e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'塞罗'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字典的取值：通过keys 进行字典的取值，unlike 列表、元组和字符串通过index取值\n",
    "奥特曼成员[10003]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79248e54",
   "metadata": {},
   "source": [
    "## 字典的一般方法\n",
    "* 1.按照字典的形式直接创建（key：value）  \n",
    "```\n",
    "奥特曼成员 = {\n",
    "    10001:\"迪迦\",\n",
    "    10002:\"泰罗\",\n",
    "    10003:\"塞罗\",\n",
    "    10004:\"梦比优斯\",\n",
    "    10005:\"迪迦\"\n",
    "}\n",
    "```\n",
    "* 2.使用dict()方法创建字典\n",
    "```\n",
    "```\n",
    "* 3.使用字典增加的方式创建字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d2395dd6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.使用dict()方法创建字典\n",
    "网新四班 = dict(\n",
    "    stu1800001=\"李一峰\",\n",
    "    stu1800002=\"Jack\",\n",
    "    stu1800003=\"Marry\",\n",
    "    stu1800004=\"周杰伦\",\n",
    "\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "0474a8a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3.使用字典增加的方式创建字典\n",
    "网新四班 = dict(\n",
    "    stu1800001=\"李一峰\",\n",
    "    stu1800002=\"Jack\",\n",
    "    stu1800003=\"Marry\",\n",
    "    stu1800004=\"周杰伦\",\n",
    "\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "55a3d492",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1800001': '李一峰',\n",
       " 'stu1800002': 'Jack',\n",
       " 'stu1800003': 'Marry',\n",
       " 'stu1800004': '周杰伦'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新四班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e8e44fdb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用字典增加的方式创建字典 : dict[key] = value\n",
    "\n",
    "网新一班 = {}\n",
    "网新一班[\"stu1800001\"] = '李一峰'\n",
    "网新一班[\"stu1800002\"] = 'Jack'\n",
    "网新一班[\"stu1800003\"] = 'Marry'\n",
    "网新一班[\"stu1800004\"] = '周杰伦'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "86f97c82",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1800001': '李一峰',\n",
       " 'stu1800002': 'Jack',\n",
       " 'stu1800003': 'Marry',\n",
       " 'stu1800004': '周杰伦'}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新一班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "056d8703",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1800001': '李一峰',\n",
       " 'stu1800002': 'Jack',\n",
       " 'stu1800003': 'Marry',\n",
       " 'stu1800004': '周杰伦'}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新一班_keys = [\"stu1800001\",\"stu1800002\",\"stu1800003\",\"stu1800004\"]\n",
    "网新一班_values = ['李一峰','Jack','Marry','周杰伦']\n",
    "网新一班 = {}\n",
    "for i in range(4):\n",
    "    网新一班[网新一班_keys[i]] = 网新一班_values[i]\n",
    "\n",
    "网新一班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c37c88ac",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
